#include "head.h"

int removefile(int net_fd, char* path, char* file_name, char* user_id, MYSQL* mysql)
{
    int type = 0;
    int ret_sqlquery;
    char str_mysql[1024] = {0};
printf("path: %s\n", path);
    
    char file_type = '\0';
    recv(net_fd, &file_type, sizeof(char), MSG_WAITALL);
    if(file_type == '?'){
        return 1;
    }
printf("file_type: %c\n", file_type);
    if(file_type !=  'd' && file_type != 'f'){
        type = 1;
        send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
        return 1;

    }
    
    if(file_type == 'd'){
        snprintf(str_mysql, sizeof(str_mysql), 
                 "select * from ( select * from file where `path` = '%s/%s' and `file_type` = '0' and `delete` = '0') x where x.Parent_dir_id = file_id", 
                 path, file_name);
printf("%s\n", str_mysql);
        ret_sqlquery = mysql_query(mysql, str_mysql);
        MYSQL_RES* res = mysql_store_result(mysql);
        if(ret_sqlquery != 0){
            type = 1;
            send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
            mysql_free_result(res);
            return 1;
        }

        int row = mysql_num_rows(res);
        if(row != 0){
            type = 2;
            send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
printf("type: %d\n", type);
            mysql_free_result(res);
            return 1;
        }
    }
printf("type: %d\n", type);
    char new_path[512] = {0};
    snprintf(new_path, sizeof(new_path), "%s/%s", path, file_name);
printf("new_path: %s\n", new_path);    
    memset(str_mysql, 0, sizeof(str_mysql));
    snprintf(str_mysql, sizeof(str_mysql), 
             "select * from file where `path` = '%s' and `file_name` = '%s'"
             " and `user_id` = '%s' and `delete` = '0' and `file_type` = '%s'", 
             new_path, file_name, user_id, (file_type == 'd') ? "0":"1");
printf("%s\n", str_mysql);
    ret_sqlquery = mysql_query(mysql, str_mysql);
    MYSQL_RES* res = mysql_store_result(mysql);
    if(ret_sqlquery != 0){
        type = 1;
        send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
        mysql_free_result(res);
        return 1;
    }

    int row = mysql_num_rows(res);
    printf("row: %d\n", row);
    if(row == 0){
        type = 1;
    
        mysql_free_result(res);

        send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
        return 1; 

    }
    memset(str_mysql, 0, sizeof(str_mysql));
    snprintf(str_mysql, sizeof(str_mysql), 
             "update file set `delete` = '1' where `path` = '%s' and `file_name` = '%s'"
             " and `user_id` = '%s' and `delete` = '0' and `file_type` = '%s'", 
             new_path, file_name, user_id, (file_type == 'd') ? "0":"1");
printf("%s\n", str_mysql);
    ret_sqlquery = mysql_query(mysql, str_mysql);
    res = mysql_store_result(mysql);
    if(ret_sqlquery != 0){
        type = 1;
        send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
        mysql_free_result(res);
        return 1;
    }

    mysql_free_result(res);
    
    send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);

    return 0;
}


